package com.example.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.example.bean.PageBean;
import com.example.dto.CategoryDto;
import com.example.entity.Category;
import com.example.mapper.CategroyMapper;
import com.example.service.CategroyService;
import com.example.utils.DateUtils;
import com.example.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 *
 * @author weijingchen
 * @DateTime 2025-09-26
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class CategroyServiceImpl implements CategroyService {

    private final CategroyMapper categroyMapper;

    @Override
    public void add(CategoryDto dto) {
        log.info("添加分类：{}", dto);
        Category category = BeanUtil.copyProperties(dto, Category.class);
        category.setCreateUser(ThreadLocalUtil.getId());
        category.setCreateTime(DateUtils.getDateTime());
        category.setUpdateTime(DateUtils.getDateTime());
        categroyMapper.add(category);
    }

    @Override
    public void update(CategoryDto dto) {
        log.info("修改分类：{}", dto);
        Category category = BeanUtil.copyProperties(dto, Category.class);
        category.setUpdateTime(DateUtils.getDateTime());
        categroyMapper.update(category);
    }

    @Override
    public void delete(Integer id) {
        log.info("删除分类：{}", id);
        categroyMapper.delete(id);
    }

    @Override
    public PageBean<Category> list(CategoryDto dto) {
        log.info("查询分类：{}", dto);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<Category> list = categroyMapper.list(dto);
        return new PageBean<>(list.getTotal(),list.getResult());
    }

    @Override
    public Category getById(Integer id) {
        return categroyMapper.getById(id);
    }
}
